package com.servlet.yanglaoyuan.controller;

import com.servlet.yanglaoyuan.common.PageResult;
import com.servlet.yanglaoyuan.common.Result;
import com.servlet.yanglaoyuan.pojo.Elderly;
import com.servlet.yanglaoyuan.pojo.ElderlyDTO;
import com.servlet.yanglaoyuan.pojo.EmergencyContact;
import com.servlet.yanglaoyuan.pojo.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.websocket.servlet.UndertowWebSocketServletWebServerCustomizer;
import org.springframework.web.bind.annotation.*;
import com.servlet.yanglaoyuan.service.elderService;

import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

/**
 * @author lingshang
 */
//老人信息controller
@Slf4j
@RestController
public class elderlyController {
    @Autowired
    private elderService elderlyService;
    //获取所有老人信息
    @GetMapping("/elderly")
    public Result findAll(
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        try {
            log.info("获取所有老人信息, page={}, pageSize={}", page, pageSize);
            PageResult<ElderlyDTO> pageResult = elderlyService.findAll(page, pageSize);
            return Result.success(pageResult);
        } catch (Exception e) {
            log.error("获取老人信息失败", e);
            return Result.error("获取老人信息失败: " + e.getMessage());
        }
    }


    /*
      添加老人信息
     */
    @PostMapping("/elderly")
    public Result add(@RequestBody ElderlyDTO elderlyDTO){
        Elderly elderly = new Elderly();
        elderly.setName(elderlyDTO.getName());
        elderly.setSex(elderlyDTO.getSex());
        elderly.setAge(elderlyDTO.getAge());
        elderly.setPhone(elderlyDTO.getPhone());
        elderly.setAddress(elderlyDTO.getAddress());
        elderly.setBloodType(elderlyDTO.getBloodType());
        elderly.setHeight(elderlyDTO.getHeight());
        elderly.setWeight(elderlyDTO.getWeight());
        elderly.setEmerName(elderlyDTO.getEmergencyContact().getName());
        elderly.setEmerPhone(elderlyDTO.getEmergencyContact().getPhone());
        elderly.setEmerRelation(elderlyDTO.getEmergencyContact().getRelationship());
         log.info("添加老人信息: {}",elderly);
         elderlyService.add(elderly);
        return Result.success();
    }
    /*
      更新老人信息
     */
    @PutMapping("/elderly/{id}")
    public Result update(@PathVariable Integer id,@RequestBody ElderlyDTO elderlyDTO){
        Elderly elderly = new Elderly();
        elderly.setId(id);
        elderly.setName(elderlyDTO.getName());
        elderly.setSex(elderlyDTO.getSex());
        elderly.setAge(elderlyDTO.getAge());
        elderly.setPhone(elderlyDTO.getPhone());
        elderly.setAddress(elderlyDTO.getAddress());
        elderly.setBloodType(elderlyDTO.getBloodType());
        elderly.setHeight(elderlyDTO.getHeight());
        elderly.setWeight(elderlyDTO.getWeight());
        elderly.setEmerName(elderlyDTO.getEmergencyContact().getName());
        elderly.setEmerPhone(elderlyDTO.getEmergencyContact().getPhone());
        elderly.setEmerRelation(elderlyDTO.getEmergencyContact().getRelationship());
        log.info("更新老人信息: {}",elderly);
        elderlyService.update(id,elderly);
        return Result.success();
    }
    //根据ID获取老人信息
    @GetMapping("/elderly/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据id获取老人信息: {}",id);
        Elderly elderly = elderlyService.getById(id);
        ElderlyDTO elderlyDTO = new ElderlyDTO(elderly.getId(),elderly.getName(),elderly.getSex(),elderly.getAge(),elderly.getPhone(),elderly.getAddress(),elderly.getBloodType(),elderly.getHeight(),elderly.getWeight(),new EmergencyContact(elderly.getEmerName(),elderly.getEmerPhone(),elderly.getEmerRelation()));
        return Result.success(elderlyDTO);
    }
    //根据手机号获取老人信息
    @GetMapping("/elderly/phone/{phone}")
    public Result getByPhone(@PathVariable String phone){
        log.info("根据手机号获取老人信息: {}",phone);
        Elderly elderly = elderlyService.getByPhone(phone);
        ElderlyDTO elderlyDTO = new ElderlyDTO(elderly.getId(),elderly.getName(),elderly.getSex(),elderly.getAge(),elderly.getPhone(),elderly.getAddress(),elderly.getBloodType(),elderly.getHeight(),elderly.getWeight(),new EmergencyContact(elderly.getEmerName(),elderly.getEmerPhone(),elderly.getEmerRelation()));
        return Result.success(elderlyDTO);
    }
    //删除老人信息
    @DeleteMapping("/elderly/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除老人信息: {}",id);
        elderlyService.delete(id);
        return Result.success();
    }

    //查询老人
    @PostMapping("/searchElderly")
    public Result searchElderly(@RequestBody Map<String, String> params) {
        List<Elderly> list =elderlyService.searchElderly(params);
        //将老人信息转换为DTO
        Stream<Object> listDTO = list.stream().map(elderly -> new ElderlyDTO(elderly.getId(),elderly.getName(),elderly.getSex(),elderly.getAge(),elderly.getPhone(),elderly.getAddress(),elderly.getBloodType(),elderly.getHeight(),elderly.getWeight(),new EmergencyContact(elderly.getEmerName(),elderly.getEmerPhone(),elderly.getEmerRelation())));
        return Result.success(listDTO);
    }

}
